package org.light.complexverb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Field;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class AddUploadDomainField extends DomainFieldVerb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		Method method = new Method();
		method.setStandardName("processRequest");
		method.setReturnType(new Type("void"));
		method.setThrowException(true);
		List<String> list = new ArrayList<String>();
		list.add("ServletException");
		list.add("IOException");
		method.setIsprotected(true);
		method.setOtherExceptions(list);
		method.addSignature(new Signature(1,"request",new Type("HttpServletRequest","javax.servlet.http")));
		method.addSignature(new Signature(2,"response",new Type("HttpServletResponse","javax.servlet.http")));
		method.addAdditionalImport("java.io.IOException");
		method.addAdditionalImport("java.io.PrintWriter");
		method.addAdditionalImport("javax.servlet.ServletException");
		method.addAdditionalImport("javax.servlet.http.HttpServlet");
		method.addAdditionalImport("javax.servlet.http.HttpServletRequest");
		method.addAdditionalImport("javax.servlet.http.HttpServletResponse");
		method.addAdditionalImport("java.util.Map");
		method.addAdditionalImport("java.util.TreeMap");
		method.addAdditionalImport("java.util.Collection");
		method.addAdditionalImport("net.sf.json.JSONObject");
		method.addAdditionalImport("net.sf.json.JsonConfig");
		method.addAdditionalImport("javax.servlet.annotation.MultipartConfig");
		method.addAdditionalImport("javax.servlet.annotation.WebServlet");
		method.addAdditionalImport("javax.servlet.http.Part");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceimplSuffix()+"."+this.domain.getStandardName()+"ServiceImpl");
		method.addAdditionalImport(this.domain.getPackageToken()+".utils.JsonImageValueProcessor");

		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,2,"response.setContentType(\"text/plain;charset=UTF-8\");"));
		sList.add(new Statement(2000L,2,"PrintWriter out = response.getWriter();"));
		sList.add(new Statement(3000L,2,"Map<String, Object> result = new TreeMap<String, Object>();"));
		sList.add(new Statement(4000L,2,"try {"));
		sList.add(new Statement(5000L,3,"byte [] imageBytes = null;"));
		sList.add(new Statement(6000L,3,"// 1. Get all parts"));
		sList.add(new Statement(7000L,3,"Collection<Part> parts = request.getParts();"));
		sList.add(new Statement(8000L,3,"for(Part part:parts) {"));
		sList.add(new Statement(9000L,4,"// 3.1 if part is multiparts \"file\""));
		sList.add(new Statement(10000L,4,"if(part.getContentType() != null){"));
		sList.add(new Statement(11000L,5,"imageBytes = part.getInputStream().readAllBytes();"));
		sList.add(new Statement(12000L,5,"if ("+this.domain.getCapFirstDomainName()+"ServiceImpl.temp"+this.domain.getCapFirstDomainName()+" == null) "+this.domain.getCapFirstDomainName()+"ServiceImpl.temp"+this.domain.getCapFirstDomainName()+" = new "+this.domain.getCapFirstDomainNameWithSuffix()+"();"));
		sList.add(new Statement(13000L,5,""+this.domain.getCapFirstDomainName()+"ServiceImpl.temp"+this.domain.getCapFirstDomainName()+".set"+this.field.getCapFirstFieldName()+"(imageBytes);"));
		sList.add(new Statement(14000L,4,"}"));
		sList.add(new Statement(15000L,3,"}"));
		sList.add(new Statement(16000L,3,"JsonConfig config = new JsonConfig();"));
		sList.add(new Statement(17000L,3,"JsonImageValueProcessor jsonImageValueProcessor = new JsonImageValueProcessor();"));
		sList.add(new Statement(18000L,3,"config.registerJsonValueProcessor(byte [].class, jsonImageValueProcessor);"));
		sList.add(new Statement(19000L,3,"result.put(\"success\", true);"));
		sList.add(new Statement(20000L,3,"result.put(\"data\", "+this.domain.getCapFirstDomainName()+"ServiceImpl.temp"+this.domain.getCapFirstDomainName()+".get"+this.field.getCapFirstFieldName()+"());"));
		sList.add(new Statement(21000L,3,"out.print(JSONObject.fromObject(result,config));"));
		sList.add(new Statement(22000L,2,"} catch (Exception e) {"));
		sList.add(new Statement(23000L,3,"e.printStackTrace();"));
		sList.add(new Statement(24000L,3,"result.put(\"success\", false);"));
		sList.add(new Statement(25000L,3,"result.put(\"data\", null);"));
		sList.add(new Statement(26000L,3,"out.print(JSONObject.fromObject(result));"));
		sList.add(new Statement(27000L,2,"} finally {"));
		sList.add(new Statement(28000L,3,"if (out != null)"));
		sList.add(new Statement(29000L,4,"out.close();"));
		sList.add(new Statement(30000L,2,"}"));

		method.setMethodStatementList(WriteableUtil.merge(sList));
		
		return method;
	}
		
	public AddUploadDomainField(Domain domain,Field field){
		super();
		this.domain = domain;
		this.field = field;
		this.verbName = "AddUpload"+this.domain.getStandardName()+this.field.getCapFirstFieldName();
		this.setLabel("新增上传");
	}

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		JavascriptBlock block = new JavascriptBlock();
		block.setStandardName("upload"+StringUtil.capFirst(this.domain.getStandardName())+this.field.getCapFirstFieldName());
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,1,"$('#add"+this.domain.getCapFirstDomainName()+this.field.getCapFirstFieldName()+"Fileupload').fileupload({"));
		sList.add(new Statement(2000L,3,"autoUpload: true,"));
		sList.add(new Statement(3000L,3,"dataType: 'json',"));
		sList.add(new Statement(4000L,3,"async: false,"));
		sList.add(new Statement(5000L,3,"success: function(data, textStatus) {"));
		sList.add(new Statement(6000L,3,"if (data.success == true){"));
		sList.add(new Statement(7000L,4,"$(\"#ff\").find(\"#"+this.field.getLowerFirstFieldName()+"\").prop(\"src\",\"data:image/png;base64,\"+data.data)"));
		sList.add(new Statement(8000L,3,"}"));
		sList.add(new Statement(9000L,3,"},"));
		sList.add(new Statement(10000L,3,"progressall: function (e, data) {"));
		sList.add(new Statement(11000L,4,"var progress = parseInt(data.loaded / data.total * 100, 10);"));
		sList.add(new Statement(12000L,3,"},"));
		sList.add(new Statement(13000L,2,"});"));
		block.setMethodStatementList(WriteableUtil.merge(sList));
		return block;

	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		JavascriptMethod method = new JavascriptMethod();
		method.setSerial(200);
		method.setStandardName("show"+this.domain.getCapFirstDomainName()+this.field.getCapFirstFieldName()+"Image");
		
		Signature s1 = new Signature(1,"value","var");
		Signature s2 = new Signature(2,"row","var");
		Signature s3 = new Signature(2,"index","var");
		method.addSignature(s1);
		method.addSignature(s2);
		method.addSignature(s3);
		
		StatementList sList = new StatementList();
		sList.add(new Statement(1000L,1,"if(row."+this.field.getLowerFirstFieldName()+"){"));
		sList.add(new Statement(2000L,2,"return \"<img style='height:50px;' border='1' src='data:image/png;base64,\"+row."+this.field.getLowerFirstFieldName()+"+\"'/>\";"));
		sList.add(new Statement(3000L,1,"} else {"));
		sList.add(new Statement(4000L,2,"return \"<img style='height:50px;' border='1' src='../images/blank.jpg'/>\";"));
		sList.add(new Statement(5000L,1,"}"));
		
		method.setMethodStatementList(sList);
		return method;	
	}
}
